<script lang="ts" setup name="XtxCarousel">
import { BannerItem } from '@/types/data'
import { onUnmounted, ref } from 'vue';
import { PropType } from 'vue'
// vue3中即便不使用ts,也可做类型, 需要使用propType方法
/* const props = defineProps({
  list: {
    type: Array as PropType<BannerItem[]>,
    required: true
  },
  autoplay: {
    type: Boolean,
    default: false
  },
  interval: {
    type: Number,
    default: 1000
  },
  isShowInd: {
    type: Boolean,
    default: false
  }
}) */
// ts方式
const { list, autoplay = false, interval = 1000, isShowInd = false } = defineProps<{
  list: BannerItem[],
  autoplay?: boolean, // 是否开启轮播
  interval?: number, // 自定义轮播切换时长
  isShowInd?: boolean // 是否显示小圆点
}>()
// 1. 默认激活第一张图片, 默认激活第一个小圆点
const activeIndex = ref(0)
// 2.1 上一张activeIndex--
const prev = () => {
  activeIndex.value--
  // 当小于0, 说明已经到第一张了, 手动设置activeIndex 为 最后一张
  if (activeIndex.value < 0) {
    activeIndex.value = list.length - 1
  }
}
// 2.2 下一张activeIndex++
const next = () => {
  activeIndex.value++
  // 当大于数组的长度 - 1(已经到4), 手动将activeIndex设置为0
  // if (activeIndex.value >= list.length) {
  if (activeIndex.value > list.length - 1) {
    activeIndex.value = 0
  }
}
// 3. 自动轮播
let timerId: number = -1
// 3.1 鼠标进入关闭自动轮播
const stop = () => {
  clearInterval(timerId)
}
// 3.2 鼠标离开再开启自动轮播
const start = () => {
  // 是否开启自动轮播
  if (autoplay) {
    stop()
    timerId = window.setInterval(() => {
      next()
    }, interval)
  }
}
start()
// 组件销毁清除定时器
onUnmounted(() => {
  stop()
})
</script>
<template>
  <div class="xtx-carousel" @mouseenter="stop" @mouseleave="start">
    <!-- 轮播图列表 -->
    <ul class="carousel-body">
      <li class="carousel-item" :class="{ fade: activeIndex === index }" v-for="(item, index) in list" :key="item.id">
        <RouterLink :to="item.hrefUrl">
          <img :src="item.imgUrl" alt="" />
        </RouterLink>
      </li>
    </ul>
    <!-- 左右箭头 -->
    <a href="javascript:;" class="carousel-btn prev" @click="prev"><i class="iconfont icon-angle-left"></i></a>
    <a href="javascript:;" class="carousel-btn next" @click="next"><i class="iconfont icon-angle-right"></i></a>
    <!-- 小圆点 -->
    <div class="carousel-indicator" v-if="isShowInd">
      <span :class="{ active: activeIndex === index }" v-for="(item, index) in list" :key="item.id"
        @mouseenter="activeIndex = index"></span>
    </div>
  </div>
</template>

<style scoped lang="less">
.xtx-carousel {
  width: 100%;
  height: 100%;
  min-width: 300px;
  min-height: 150px;
  position: relative;

  .carousel {
    &-body {
      width: 100%;
      height: 100%;
    }

    &-item {
      width: 100%;
      height: 100%;
      position: absolute;
      left: 0;
      top: 0;
      opacity: 0;
      transition: opacity 0.5s linear;

      &.fade {
        opacity: 1;
        z-index: 1;
      }

      img {
        width: 100%;
        height: 100%;
      }
    }

    &-indicator {
      position: absolute;
      left: 0;
      bottom: 20px;
      z-index: 2;
      width: 100%;
      text-align: center;

      span {
        display: inline-block;
        width: 12px;
        height: 12px;
        background: rgba(0, 0, 0, 0.2);
        border-radius: 50%;
        cursor: pointer;

        ~span {
          margin-left: 12px;
        }

        &.active {
          background: #fff;
        }
      }
    }

    &-btn {
      width: 44px;
      height: 44px;
      background: rgba(0, 0, 0, 0.2);
      color: #fff;
      border-radius: 50%;
      position: absolute;
      top: 228px;
      z-index: 2;
      text-align: center;
      line-height: 44px;
      opacity: 0;
      transition: all 0.5s;

      &.prev {
        left: 20px;
      }

      &.next {
        right: 20px;
      }
    }
  }

  &:hover {
    .carousel-btn {
      opacity: 1;
    }
  }
}
</style>